// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
	env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
	const {
		formData,
		_id,
		image,
		second_category_index
	} = event

	switch (second_category_index) {
		case 0:
			return await db.collection('lost_and_found_infos').doc(_id).update({
				data: {
					student_name: formData.student_name,
					student_ID: formData.student_ID,
					location: formData.location,
					contact_way: formData.contact_way
				}
			})
			break
		case 1:
			return await db.collection('lost_and_found_infos').doc(_id).update({
				data: {
					student_name: formData.student_name,
					ID_card_No: formData.ID_card_No,
					location: formData.location,
					contact_way: formData.contact_way
				}
			})
			break
		case 2:
			if (image == '') {
				return await db.collection('lost_and_found_infos').doc(_id).update({
					data: {
						movie_card_No: formData.movie_card_No,
						location: formData.location,
						contact_way: formData.contact_way
					}
				})
			} else {
				return await db.collection('lost_and_found_infos').doc(_id).update({
					data: {
						movie_card_No: formData.movie_card_No,
						location: formData.location,
						contact_way: formData.contact_way,
						image: [image]
					}
				})
			}

			break
		case 3:
			return await db.collection('lost_and_found_infos').doc(_id).update({
				data: {
					bank_card_name: formData.bank_card_name,
					bank_card_No: formData.bank_card_No,
					location: formData.location,
					contact_way: formData.contact_way
				}
			})
			break
		default:
			if (image == '') {
				return await db.collection('lost_and_found_infos').doc(_id).update({
					data: {
						item_name: formData.item_name,
						description: formData.description,
						location: formData.location,
						contact_way: formData.contact_way
					}
				})
			} else {
				return await db.collection('lost_and_found_infos').doc(_id).update({
					data: {
						item_name: formData.item_name,
						description: formData.description,
						location: formData.location,
						contact_way: formData.contact_way,
						image: [image]
					}
				})
			}
			break
	}
}
